package com.xishu.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xishu.bo.LockOrderCashOrOrderItemBo;
import com.xishu.bo.MoveItem;
import com.xishu.bo.SubOrder;
import com.xishu.entity.User;
import com.xishu.entity.oplog.OrderOpLog;
import com.xishu.entity.order.*;
import com.xishu.entity.shop.*;
import com.xishu.service.order.base.OrderService;
import com.xishu.util.ServletUtil;
import com.xishu.util.Tools;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;
import java.util.stream.Collectors;

public class OrderOpLogService implements Tools {
    private static OrderOpLogService instance = new OrderOpLogService();
    private static Logger logger = LoggerFactory.getLogger(OrderOpLogService.class);
    private CommonService commonService = CommonService.getInstance();

    private OrderOpLogService() {
    }

    public static OrderOpLogService getInstance() {
        return instance;
    }

    /**
     * 处理一些固定参数
     *
     * @param order
     * @param content
     * @return
     */
    public OrderOpLog content(Order order, Long content) {
        OrderOpLog orderOpLog = new OrderOpLog();
        try {

            //unicode
            if (isNotEmpty(order.getUniqueCode())) {
                orderOpLog.setUniqueCode(order.getUniqueCode());
            }
            //订单编号
            if (isNotEmpty(order.getOrderNo())) {
                orderOpLog.setOrderNo(order.getOrderNo());
            }

            //同步uniqueCode相同的开台时间
            orderOpLog.setOrderCreateTime(order.getCreateTime());
            orderOpLog.setOrderCreateDate(order.getCreateDate());

            //外卖联系人
            if (isNotEmpty(order.getContact())) {
                orderOpLog.setContact(order.getContact());
            }

            User user = ServletUtil.getUser();
            if (user != null) {
                orderOpLog.setUserName(user.getName());
            }

            //是否发送到厨房
            if (isNotEmpty(order.getSendKitchen())) {
                orderOpLog.setSendToKitchen(true);
            }

            //订单id
            orderOpLog.setOrderId(order.getId());

            //桌号
            if (isNotEmpty(order.getShowNo())) {
                orderOpLog.setCode(order.getCode()+"-"+order.getShowNo());
            }else {
                orderOpLog.setCode(order.getCode());
            }


            //显示的桌号
            if (isNotEmpty(order.getShowNo())) {
                orderOpLog.setShowNo(order.getShowNo());
            }

            //内容
            orderOpLog.setOpContent(content);

            //订单类型（父订单，子订单，支付订单）
            if (order.getParentOrder()) {
                orderOpLog.setOrderType("父訂單");
            } else if (order.getSubOrder()) {
                orderOpLog.setOrderType("子訂單");
            } else {
                orderOpLog.setOrderType("支付訂單");
            }

            //公司id
            orderOpLog.setCompanyId(order.getCompanyId());

            //公司
            orderOpLog.setCompanyName(order.getCompanyName());

            //分店
            orderOpLog.setShopName(order.getShopName());
            orderOpLog.setShopName_zh(order.getShopName_zh());
            orderOpLog.setShopName_en(order.getShopName_en());

            //分店id
            orderOpLog.setShopId(order.getShopId());

            //IP 地址
            orderOpLog.setIpAddress(ServletUtil.getRemoteIp(ServletUtil.getRequest()));

        } catch (Exception e) {
            logger.error("e", e);
        }

        return orderOpLog;
    }


    //处理细节的方法

    /**
     * 创建订单的内容
     *
     * @param order
     * @param orderOpLog
     * @return
     */
    public OrderOpLog createOrderLog(Order order, OrderOpLog orderOpLog) {
        try {
            orderOpLog.setCode(order.getCode());
            //人数
            String person = order.getPersons().toString();
            List<String> contentList = new ArrayList<>();
            LinkedHashMap<String, String> map = new LinkedHashMap<>();
            orderOpLog.setDetailContent("人數" + person);
            Object obj = map;
            String stringObj = JSON.toJSON(obj).toString();
            contentList.add(stringObj);
            String value = JSON.toJSONString(contentList);
            orderOpLog.setDetailContent(value);
            commonService.createObject(orderOpLog);
        } catch (Exception e) {
            logger.error("e", e);
        }
        return orderOpLog;
    }

    /**
     * 创建外卖订单
     *
     * @param order
     * @param orderOpLog
     * @return
     */
    public OrderOpLog takeOutOrderLog(Order order, OrderOpLog orderOpLog) {
        try {
            OrderService orderService = OrderService.getInstance();
            if (orderService.isTakeoutOrder(order)) {
                orderOpLog.setTakeout(true);
                List<String> contentList = new ArrayList<>();
                LinkedHashMap<String, String> map = new LinkedHashMap<>();
                orderOpLog.setDetailContent("訂單編號" + order.getOrderNo().toString());
                Object obj = map;
                String stringObj = JSON.toJSON(obj).toString();
                contentList.add(stringObj);
                String value = JSON.toJSONString(contentList);
                orderOpLog.setDetailContent(value);
                commonService.createObject(orderOpLog);
            }
        } catch (Exception e) {
            logger.error("e", e);
        }
        return orderOpLog;
    }


    /**
     * 减orderItem内容
     *
     * @param orderItemList
     * @param orderOpLogContent
     * @return
     */
    public OrderOpLog subOrderItemLog(List<OrderItem> orderItemList, OrderOpLog orderOpLogContent) {

        try {
            if (isNotEmpty(orderItemList)) {
                OrderItem orderItem = orderItemList.get(0);
                List<String> contentList = new ArrayList<>();
                LinkedHashMap<String, String> map = new LinkedHashMap<>();
                orderOpLogContent.setAddItem(true);
                orderOpLogContent.setItemName(orderItem.getItemName());
                map.put("减菜名称",orderItem.getItemName());
                map.put("數量", "-"+orderItem.getCount().toString());
                if (isNotEmpty(orderItem.getBasePrice())) {
                    map.put("原始價格", orderItem.getBasePrice().toString());
                }

                if (isNotEmpty(orderItem.getItemTotalCost())) {
                    map.put("總計價格", orderItem.getItemTotalCost().toString());
                }
                Object obj = map;
                String stringObj = JSON.toJSON(obj).toString();
                contentList.add(stringObj);
                //是否是套餐，是就把setMenuOneItemList写到内容里边
                String jsonString = disposeDetail(orderItem, contentList);
                orderOpLogContent.setDetailContent(jsonString);
                commonService.createObject(orderOpLogContent);
            }
        } catch (Exception e) {
            logger.error("e", e);
        }

        return orderOpLogContent;
    }


    /**
     * 减orderItemCount
     *
     * @param orderItem
     * @param orderOpLogContent
     * @return
     */
    public OrderOpLog subOrderItemCountLog(OrderItem orderItem, OrderOpLog orderOpLogContent) {

        try {
            List<String> contentList = new ArrayList<>();
            LinkedHashMap<String, String> map = new LinkedHashMap<>();
            orderOpLogContent.setAddItem(true);
            orderOpLogContent.setItemName(orderItem.getItemName());
            map.put("减菜名称",orderItem.getItemName());
            map.put("數量", "-1");
            if (isNotEmpty(orderItem.getBasePrice())) {
                map.put("原始價格", orderItem.getBasePrice().toString());
            }
            if (isNotEmpty(orderItem.getItemTotalCost())) {
                map.put("總計價格", orderItem.getItemTotalCost().toString());
            }
            Object obj = map;
            String stringObj = JSON.toJSON(obj).toString();
            contentList.add(stringObj);
            String jsonString = disposeDetail(orderItem, contentList);

            orderOpLogContent.setDetailContent(jsonString);
            commonService.createObject(orderOpLogContent);
        } catch (Exception e) {
            logger.error("e", e);
        }


        return orderOpLogContent;
    }


    /**
     * 加菜加加数量操作
     *
     * @param orderItem
     * @param orderOpLogContent
     * @return
     */
    public OrderOpLog addOrderItemCountLog(OrderItem orderItem, OrderOpLog orderOpLogContent) {

        try {
            List<String> contentList = new ArrayList<>();
            LinkedHashMap<String, String> map = new LinkedHashMap<>();
            orderOpLogContent.setAddItem(true);
            orderOpLogContent.setItemName(orderItem.getItemName());
            map.put("加菜名称",orderItem.getItemName());
            map.put("數量", "+1");
            if (isNotEmpty(orderItem.getBasePrice())) {
                map.put("原始價格", orderItem.getBasePrice().toString());
            }
            if (isNotEmpty(orderItem.getItemTotalCost())) {
                map.put("總計價格", orderItem.getItemTotalCost().toString());
            }
            Object obj = map;
            String stringObj = JSON.toJSON(obj).toString();
            contentList.add(stringObj);
            //是否是套餐，是就把setMenuOneItemList写到内容里边
            String jsonString = disposeDetail(orderItem, contentList);

            orderOpLogContent.setDetailContent(jsonString);
            commonService.createObject(orderOpLogContent);
        } catch (Exception e) {
            logger.error("e", e);
        }

        return orderOpLogContent;
    }


    /**
     * 加菜，加orderItem操作
     *
     * @param orderItem
     * @param orderOpLogContent
     * @return
     */
    public OrderOpLog addOrderItemLog(OrderItem orderItem, OrderOpLog orderOpLogContent) {
        try {
            List<String> contentList = new ArrayList<>();
            LinkedHashMap<String, String> map = new LinkedHashMap<>();
            orderOpLogContent.setAddItem(true);
            orderOpLogContent.setItemName(orderItem.getItemName());
            map.put("加菜名称",orderItem.getItemName());
            map.put("數量", "+1");
            if (isNotEmpty(orderItem.getBasePrice())) {
                map.put("原始價格", orderItem.getBasePrice().toString());
            }
            if (isNotEmpty(orderItem.getItemTotalCost())) {
                map.put("總計價格", orderItem.getItemTotalCost().toString());
            }
            Object obj = map;
            String stringObj = JSON.toJSON(obj).toString();
            contentList.add(stringObj);
            //是否是套餐，是就把setMenuOneItemList写到内容里边
            String jsonString = disposeDetail(orderItem, contentList);

            orderOpLogContent.setDetailContent(jsonString);
            commonService.createObject(orderOpLogContent);
        } catch (Exception e) {
            logger.error("e", e);
        }

        return orderOpLogContent;
    }

    /**
     * 显示orderItem细节信息
     *
     * @param orderItem
     * @param contentList
     * @return
     */
    private String disposeDetail(OrderItem orderItem, List<String> contentList) {
        String jsonString = null;
        try {
            if (getBoolean(orderItem.getSetMenu())) {
                if (isNotEmpty(orderItem.getSetMenuOneItemList())) {
                    List<SetMenuOneItem> setMenuOneItemList = orderItem.getSetMenuOneItemList();
                    for (int i = 0; i < setMenuOneItemList.size(); i++) {
                        LinkedHashMap<String, String> map = new LinkedHashMap<>();
                        SetMenuOneItem setMenuOneItem = setMenuOneItemList.get(i);
                        for (Item item : setMenuOneItem.getItemList()) {
                            map.put("菜名稱", item.getName_zh());
                            map.put("總加價", String.valueOf((getDouble(item.getSetMenuAddPrice())* getInt(item.getCountInOrder()))));
                            if (isNotEmpty(item.getCountInOrder())){
                                map.put("加入購物車數量",getInt(item.getCountInOrder()).toString());
                            }
                            Object obj = map;
                            String stringObj = JSON.toJSON(obj).toString();
                            contentList.add(stringObj);
                            List<ItemDetail> itemDetailList = item.getItemDetailList();
                            //处理套餐下的细节菜
                            detailItemContent(contentList, itemDetailList);
                        }

                    }

                }

            } else
                //处理细节菜
                if (isNotEmpty(orderItem.getItemDetailList())) {
                    List<ItemDetail> itemDetailList = orderItem.getItemDetailList();
                    detailItemContent(contentList, itemDetailList);
                } else
                //其他都不是，那就是普通菜了
                {
                    LinkedHashMap<String, String> map = new LinkedHashMap<>();
                    map.put("菜名", orderItem.getItemName());
                    map.put("價格", orderItem.getItemPrice().toString());
                    Object obj = map;
                    String stringObj = JSON.toJSON(obj).toString();
                    contentList.add(stringObj);
                }

            jsonString = JSON.toJSON(contentList).toString();

        } catch (Exception e) {
            logger.error("e", e);
        }
        return jsonString;
    }

    /**
     * 订单日志细节菜处理
     * @param contentList
     * @param itemDetailList
     */
    private void detailItemContent(List<String> contentList, List<ItemDetail> itemDetailList) {
        try {
            if (isNotEmpty(itemDetailList)) {
                for (int i = 0; i < itemDetailList.size(); i++) {
                    LinkedHashMap<String, String> map = new LinkedHashMap<>();
                    ItemDetail itemDetail = itemDetailList.get(i);
                    map.put("細節名稱", itemDetail.getName());
                    Object obj = map;
                    String stringObj = JSON.toJSON(obj).toString();
                    contentList.add(stringObj);
                    //菜品规格处理
                    itemSpecification(contentList, itemDetail);


                }
            }
        } catch (Exception e) {
            logger.error("e", e);
        }
    }

    /**
     * 菜品细节规格处理
     * @param contentList
     * @param itemDetail
     */
    private void itemSpecification(List<String> contentList, ItemDetail itemDetail) {
        try {
            for (NameAndPrice nameAndPrice : itemDetail.getNameAndPriceList()) {
                LinkedHashMap<String, String> map = new LinkedHashMap<>();
                if (isNotEmpty(nameAndPrice.getName())){
                    map.put("名稱", nameAndPrice.getName());
                }else if (isNotEmpty(nameAndPrice.getName_zh())){
                    map.put("名稱", nameAndPrice.getName_zh());
                }else {
                    map.put("名稱", nameAndPrice.getName_en());
                }
                map.put("加價", showMoneyDouble(nameAndPrice.getPrice()).toString());
                Object obj = map;
                String stringObj = JSON.toJSON(obj).toString();
                contentList.add(stringObj);
            }
        } catch (Exception e) {
            logger.error("e", e);
        }
    }

    /**
     * c端分菜，多个orderItem详细日志
     * @param orderItemList
     * @param contentList
     * @return
     */
    private String disposeDetailList(List<OrderItem> orderItemList, List<String> contentList) {
        String jsonString = null;
        try {
            for (OrderItem orderItem : orderItemList) {
                if (getBoolean(orderItem.getSetMenu())) {

                    if (isNotEmpty(orderItem.getSetMenuOneItemList())) {

                        List<SetMenuOneItem> setMenuOneItemList = orderItem.getSetMenuOneItemList();
                        for (int i = 0; i < setMenuOneItemList.size(); i++) {
                            LinkedHashMap<String, String> map = new LinkedHashMap<>();
                            map.put("菜名", orderItem.getItemName());
                            map.put("價格", orderItem.getItemPrice().toString());
                            SetMenuOneItem setMenuOneItem = setMenuOneItemList.get(i);
                            for (Item item : setMenuOneItem.getItemList()) {
                                map.put("名稱", item.getName());
                                map.put("加價", item.getSetMenuAddPrice().toString());
                            }
                            Object obj = map;
                            String stringObj = JSON.toJSON(obj).toString();
                            contentList.add(stringObj);
                        }

                    }

                } else
                    //处理细节菜
                    if (isNotEmpty(orderItem.getItemDetailList())) {
                        List<ItemDetail> itemDetailList = orderItem.getItemDetailList();
                        for (int i = 0; i < itemDetailList.size(); i++) {
                            LinkedHashMap<String, String> map = new LinkedHashMap<>();
                            map.put("菜名", orderItem.getItemName());
                            map.put("價格", orderItem.getItemPrice().toString());
                            ItemDetail itemDetail = itemDetailList.get(i);
                            map.put("細節名稱", itemDetail.getName());
                            for (NameAndPrice nameAndPrice : itemDetail.getNameAndPriceList()) {
                                map.put("名稱", nameAndPrice.getName());
                                map.put("加價", showMoneyDouble(nameAndPrice.getPrice()).toString());
                            }
                            Object obj = map;
                            String stringObj = JSON.toJSON(obj).toString();
                            contentList.add(stringObj);
                        }
                    } else
                    //其他都不是，那就是普通菜了
                    {
                        LinkedHashMap<String, String> map = new LinkedHashMap<>();
                        map.put("菜名", orderItem.getItemName());
                        map.put("價格", orderItem.getItemPrice().toString());
                        Object obj = map;
                        String stringObj = JSON.toJSON(obj).toString();
                        contentList.add(stringObj);
                    }
            }

            jsonString = JSON.toJSON(contentList).toString();

        } catch (Exception e) {
            logger.error("e", e);
        }
        return jsonString;
    }




    /**
     * 发送厨房
     *
     * @param orderInDb
     * @param orderOpLog
     * @return
     */
    public OrderOpLog sendKitchenLog(Order orderInDb, OrderOpLog orderOpLog) {
        try {
            List<String> contentList = new ArrayList<>();
            List<SendPrinter> sendPrinterList = orderInDb.getSendPrinterList();
            //已发送厨房的orderItemId
            List<Long> orderItemId = sendPrinterList.stream().map(s -> s.getOrderItemId()).collect(Collectors.toList());
            //订单详情的orderItemId
            List<OrderItem> orderItemDetailList = orderInDb.getOrderItemDetailList();
            List<OrderItem> items = orderItemDetailList.stream().filter(s -> !orderItemId.contains(s.getId())).collect(Collectors.toList());

            for (OrderItem item : items) {

                LinkedHashMap<String, String> map = new LinkedHashMap<>();
                map.put(item.getItemName(), item.getCount().toString());
                Object obj = map;
                String stringObj = JSON.toJSON(obj).toString();
                contentList.add(stringObj);
            }
            String value = JSON.toJSONString(contentList);
            orderOpLog.setDetailContent(value);
            commonService.createObject(orderOpLog);
        } catch (Exception e) {
            logger.error("e", e);
        }
        return orderOpLog;
    }

    /**
     * 发送厨房单个菜形式发送
     * @param orderInDb
     * @param orderItemIdList
     * @param orderOpLog
     * @return
     */
    public OrderOpLog realSendKitchenLog(Order orderInDb, List<Long> orderItemIdList , OrderOpLog orderOpLog) {
        try {
            List<String> contentList = new ArrayList<>();
            List<OrderItem> orderItemList = getList(orderInDb.getOrderItemDetailList()).stream().filter(s -> getList(orderItemIdList).contains(s.getId())).collect(Collectors.toList());
            for (OrderItem item : getList(orderItemList)) {
                LinkedHashMap<String, String> map = new LinkedHashMap<>();
                map.put(item.getItemName(), item.getCount().toString());
                Object obj = map;
                String stringObj = JSON.toJSON(obj).toString();
                contentList.add(stringObj);
            }
            String value = JSON.toJSONString(contentList);
            orderOpLog.setDetailContent(value);
            commonService.createObject(orderOpLog);
        } catch (Exception e) {
            logger.error("e", e);
        }
        return orderOpLog;
    }


    /**
     * 撤销订单
     *
     * @param order
     * @param orderOpLog
     * @return
     */
    public OrderOpLog cancelPayTypeLog(Order order, OrderOpLog orderOpLog) {
        try {

            List<String> contentList = new ArrayList<>();
            LinkedHashMap<String, String> map = new LinkedHashMap<>();
            map.put("撤銷了這個訂單", order.getId().toString());
            Object obj = map;
            String stringObj = JSON.toJSON(obj).toString();
            contentList.add(stringObj);
            String value = JSON.toJSONString(contentList);
            orderOpLog.setDetailContent(value);
            commonService.createObject(orderOpLog);
        } catch (Exception e) {
            logger.error("e", e);
        }

        return orderOpLog;
    }

    /**
     * 订单支付成功
     *
     * @param parentOrder
     * @param orderOpLog
     * @return
     */
    public OrderOpLog orderPayOkLog(Order parentOrder, OrderOpLog orderOpLog) {
        try {
            List<String> contentList = new ArrayList<>();
            LinkedHashMap<String, String> map = new LinkedHashMap<>();
            map.put("訂單支付成功", parentOrder.getId().toString());
            List<String> payTypeNameList = getList(parentOrder.getPayTypeNameList()).stream().map(s -> s.getName_zh()).collect(Collectors.toList());
            map.put("支付方式",payTypeNameList.toString());
            Object obj = map;
            String stringObj = JSON.toJSON(obj).toString();
            contentList.add(stringObj);
            String value = JSON.toJSONString(contentList);
            orderOpLog.setDetailContent(value);
            Optional<OrderOpLog> opLogOptional = commonService.searchOneOptional(orderOpLog);
            if (opLogOptional.isPresent()){
                return opLogOptional.get();
            }
            commonService.createObject(orderOpLog);
        } catch (Exception e) {
            logger.error("e", e);
        }
        return orderOpLog;
    }
    /**
     * 订单支付成功
     *
     * @param subOrder
     * @param orderOpLog
     * @return
     */
    public OrderOpLog subOrderPayOkLog(Order subOrder, OrderOpLog orderOpLog) {
        try {
            List<String> contentList = new ArrayList<>();
            LinkedHashMap<String, String> map = new LinkedHashMap<>();
            map.put("子訂單支付成功", subOrder.getId().toString());
            map.put("支付方式", subOrder.getPayTypeName_zh());
            Object obj = map;
            String stringObj = JSON.toJSON(obj).toString();
            contentList.add(stringObj);
            String value = JSON.toJSONString(contentList);
            orderOpLog.setDetailContent(value);
            Optional<OrderOpLog> opLogOptional = commonService.searchOneOptional(orderOpLog);
            if (opLogOptional.isPresent()) {
                return opLogOptional.get();
            }
            commonService.createObject(orderOpLog);
        } catch (Exception e) {
            logger.error("e", e);
        }
        return orderOpLog;
    }

    /**
     * 分菜结账按照人数分
     *
     * @param orderInDb
     * @param orderOpLog
     * @return
     */
    public OrderOpLog PartCashPersonLog(Order orderInDb, OrderOpLog orderOpLog) {
        try {
            PartCash partCash = orderInDb.getPartCash();
            if (isNotEmpty(partCash.getPersons())) {
                List<String> contentList = new ArrayList<>();
                LinkedHashMap<String, String> map = new LinkedHashMap<>();
                map.put("按照人數分錢結賬", partCash.getPersons().toString());
                Object obj = map;
                String stringObj = JSON.toJSON(obj).toString();
                contentList.add(stringObj);
                String value = JSON.toJSONString(contentList);
                orderOpLog.setDetailContent(value);
                commonService.createObject(orderOpLog);
            }
        } catch (Exception e) {
            logger.error("e", e);
        }
        return orderOpLog;
    }

    /**
     * 按金额分钱结账，自定义结账
     *
     * @param
     * @param subOrder
     * @param orderOpLog
     * @return
     */
    public OrderOpLog PartCashMoneyLog(SubOrder subOrder, OrderOpLog orderOpLog) {
        try {

            List<String> contentList = new ArrayList<>();
            LinkedHashMap<String, String> map = new LinkedHashMap<>();
            map.put("按照金額分錢結賬", subOrder.getUserCost().toString());
            Object obj = map;
            String stringObj = JSON.toJSON(obj).toString();
            contentList.add(stringObj);
            String value = JSON.toJSONString(contentList);
            orderOpLog.setDetailContent(value);
            commonService.createObject(orderOpLog);
        } catch (Exception e) {
            logger.error("e", e);
        }
        return orderOpLog;
    }

    /**
     * 订单退款log
     *
     * @param subOrder
     * @param orderOpLog
     * @return
     */
    public OrderOpLog orderReturnCashLog(Order subOrder, OrderOpLog orderOpLog) {
        try {
            List<String> contentList = new ArrayList<>();
            LinkedHashMap<String, String> map = new LinkedHashMap<>();
            map.put("訂單退款了", subOrder.getId().toString());
            Object obj = map;
            String stringObj = JSON.toJSON(obj).toString();
            contentList.add(stringObj);
            String value = JSON.toJSONString(contentList);
            orderOpLog.setDetailContent(value);
            commonService.createObject(orderOpLog);
        } catch (Exception e) {
            logger.error("e", e);
        }

        return orderOpLog;
    }

    /**
     * 子订单移动菜，用于分菜
     *
     * @param moveItem
     * @param toOrder
     * @param orderOpLog
     * @return
     */
    public OrderOpLog moveItemToOrderLog(MoveItem moveItem, Order toOrder, OrderOpLog orderOpLog) {
        try {
            List<String> contentList = new ArrayList<>();
            LinkedHashMap<String, String> map = new LinkedHashMap<>();
            map.put(moveItem.getFromOrderId().toString() + "訂單移動到", toOrder.getId().toString());
            //移动了这些菜
            List<OrderItem> orderItemList = toOrder.getOrderItemDetailList().stream().filter(s -> moveItem.getOrderItemIdList().contains(s.getId())).collect(Collectors.toList());

            for (int i = 0; i < orderItemList.size(); i++) {
                OrderItem orderItem = orderItemList.get(i);
                map.put("第" + (i + 1) + "個菜", orderItem.getItemName());
            }
            Object obj = map;
            String stringObj = JSON.toJSON(obj).toString();
            contentList.add(stringObj);
            String value = JSON.toJSONString(contentList);
            orderOpLog.setDetailContent(value);
            commonService.createObject(orderOpLog);
        } catch (Exception e) {
            logger.error("e", e);
        }
        return orderOpLog;
    }

    /**
     * 打印订单
     *
     * @param orderInDb
     * @param orderOpLog
     * @return
     */
    public OrderOpLog printOrderLog(Order orderInDb, OrderOpLog orderOpLog) {
        try {

            List<String> contentList = new ArrayList<>();
            LinkedHashMap<String, String> map = new LinkedHashMap<>();
            map.put("打印了訂單", orderInDb.getId().toString());
            Object obj = map;
            String stringObj = JSON.toJSON(obj).toString();
            contentList.add(stringObj);
            String value = JSON.toJSONString(contentList);
            orderOpLog.setDetailContent(value);
            commonService.createObject(orderOpLog);
        } catch (Exception e) {
            logger.error("e", e);
        }
        return orderOpLog;
    }

    /**
     * 更换支付方式
     *
     * @param payOrder
     * @param orderOpLog
     * @return
     */
    public OrderOpLog changePayTypeLog(Order payOrder, OrderOpLog orderOpLog) {
        try {
            PayType payType = PayTypeService.getInstance().filterPayType(payOrder.getShopId(), payOrder.getPayType());
            List<String> contentList = new ArrayList<>();
            LinkedHashMap<String, String> map = new LinkedHashMap<>();
            map.put("更換支付方式為", payType.getName());
            Object obj = map;
            String stringObj = JSON.toJSON(obj).toString();
            contentList.add(stringObj);
            String value = JSON.toJSONString(contentList);
            orderOpLog.setDetailContent(value);
            commonService.createObject(orderOpLog);
        } catch (Exception e) {
            logger.error("e", e);
        }
        return orderOpLog;
    }


    /**
     * 添加特别菜
     *
     * @param reqOrder
     * @param orderOpLog
     * @return
     */
    public OrderOpLog addSpecialItemLog(Order reqOrder, OrderOpLog orderOpLog) {
        try {
            List<OrderItem> orderItemDetailList = reqOrder.getOrderItemDetailList();
            List<String> contentList = new ArrayList<>();
            LinkedHashMap<String, String> map = new LinkedHashMap<>();
            for (OrderItem orderItem : orderItemDetailList) {
                map.put("特別菜", orderItem.getItemName());
                map.put("價  格", orderItem.getItemPrice().toString());
            }

            Object obj = map;
            String stringObj = JSON.toJSON(obj).toString();
            contentList.add(stringObj);
            String value = JSON.toJSONString(contentList);
            orderOpLog.setDetailContent(value);
            commonService.createObject(orderOpLog);
        } catch (Exception e) {
            logger.error("e", e);
        }

        return orderOpLog;
    }

    /**
     * 进入分钱结账
     * @param orderInDb
     * @param orderOpLog
     * @return
     */
    public OrderOpLog toPartCash(Order orderInDb, OrderOpLog orderOpLog) {

        try {
            List<String> contentList = new ArrayList<>();
            LinkedHashMap<String, String> map = new LinkedHashMap<>();
            map.put("進入了分錢結賬", orderInDb.getId().toString());
            Object obj = map;
            String stringObj = JSON.toJSON(obj).toString();
            contentList.add(stringObj);
            String value = JSON.toJSONString(contentList);
            orderOpLog.setDetailContent(value);
            commonService.createObject(orderOpLog);
        } catch (Exception e) {
            logger.error("e",e);
        }
        return orderOpLog;
    }

    /**
     * 进入分菜结账
     * @param orderInDb
     * @param orderOpLog
     * @return
     */
    public OrderOpLog toPartItem(Order orderInDb, OrderOpLog orderOpLog) {

        try {
            List<String> contentList = new ArrayList<>();
            LinkedHashMap<String, String> map = new LinkedHashMap<>();
            map.put("進入了分菜結賬", orderInDb.getId().toString());
            Object obj = map;
            String stringObj = JSON.toJSON(obj).toString();
            contentList.add(stringObj);
            String value = JSON.toJSONString(contentList);
            orderOpLog.setDetailContent(value);
            commonService.createObject(orderOpLog);
        } catch (Exception e) {
            logger.error("e",e);
        }
        return orderOpLog;
    }


    /**
     * 同意落单
     * @param sendToKitchenAudit
     * @param orderOpLog
     * @return
     */
    public OrderOpLog agreeBeAlone(SendToKitchenAudit sendToKitchenAudit, OrderOpLog orderOpLog) {

        try {
            List<OrderItem> orderItemDetailList = sendToKitchenAudit.getOrderItemDetailList();
            JSONArray jsonArray = new JSONArray();
            for (OrderItem orderItem : orderItemDetailList) {
                List<String> contentList = new ArrayList<>();
                LinkedHashMap<String, String> map = new LinkedHashMap<>();
                orderOpLog.setAddItem(true);
                orderOpLog.setItemName(orderItem.getItemName());
                map.put("名称", orderItem.getItemName());
                map.put("數量", orderItem.getCount().toString());
                Object obj = map;
                String stringObj = JSON.toJSON(obj).toString();
                contentList.add(stringObj);
                String disposeDetail = disposeDetail(orderItem, contentList);
                //处理多个json对象
                JSONArray contextJson = JSONObject.parseArray(disposeDetail);
                jsonArray.addAll(contextJson);
            }
            String jsonToString = JSON.toJSON(jsonArray).toString();
            orderOpLog.setDetailContent(jsonToString);
            commonService.createObject(orderOpLog);
        } catch (Exception e) {
            logger.error("e",e);
        }
        return orderOpLog;
    }

    /**
     * 拒绝落单
     * @param sendToKitchenAudit
     * @param orderOpLog
     * @return
     */
    public OrderOpLog refuseBeAlone(SendToKitchenAudit sendToKitchenAudit, OrderOpLog orderOpLog) {
        try {
            List<OrderItem> orderItemDetailList = sendToKitchenAudit.getOrderItemDetailList();
            JSONArray jsonArray = new JSONArray();
            for (OrderItem orderItem : orderItemDetailList) {
                List<String> contentList = new ArrayList<>();
                LinkedHashMap<String, String> map = new LinkedHashMap<>();
                orderOpLog.setAddItem(true);
                orderOpLog.setItemName(orderItem.getItemName());
                map.put("名称", orderItem.getItemName());
                map.put("數量", orderItem.getCount().toString());
                Object obj = map;
                String stringObj = JSON.toJSON(obj).toString();
                contentList.add(stringObj);
                String disposeDetail = disposeDetail(orderItem, contentList);
                //处理多个json对象
                JSONArray contextJson = JSONObject.parseArray(disposeDetail);
                jsonArray.addAll(contextJson);
            }
            String jsonToString = JSON.toJSON(jsonArray).toString();
            orderOpLog.setDetailContent(jsonToString);
            commonService.createObject(orderOpLog);
        } catch (Exception e) {
            logger.error("e",e);
        }
        return orderOpLog;
    }

    public void partItemPay(Order orderInDb, LockOrderCashOrOrderItemBo lockOrderCashOrOrderItemBo, OrderOpLog orderOpLog) {

        try {
            List<String> contentList = new ArrayList<>();
            LinkedHashMap<String, String> map = new LinkedHashMap<>();
            map.put("分菜結賬", orderInDb.getId().toString());
            Object obj = map;
            String stringObj = JSON.toJSON(obj).toString();
            contentList.add(stringObj);
            ArrayList<OrderItem> orderItemList = new ArrayList<>();
            List<Long> orderItemIdList = lockOrderCashOrOrderItemBo.getOrderItemIdList();
            //有几个添加几个，只用于c端
            for (Long orderItemId : orderItemIdList) {
                for (OrderItem orderItemInDb : orderInDb.getOrderItemDetailList()) {
                    if (equals(orderItemId,orderItemInDb.getId())){
                        orderItemList.add(orderItemInDb);
                    }
                }
            }
            String jsonString = disposeDetailList(orderItemList, contentList);
            orderOpLog.setDetailContent(jsonString);
            commonService.createObject(orderOpLog);
        } catch (Exception e) {
            logger.error("e",e);
        }

    }

    /**
     * 删除订单
     * @param order
     * @param orderOpLog
     */
    public void deleteOrder(Order order, OrderOpLog orderOpLog) {
        try {
            List<String> contentList = new ArrayList<>();
            LinkedHashMap<String, String> map = new LinkedHashMap<>();
            map.put("刪除了訂單", order.getId().toString());
            Object obj = map;
            String stringObj = JSON.toJSON(obj).toString();
            contentList.add(stringObj);
            String value = JSON.toJSONString(contentList);
            orderOpLog.setDetailContent(value);
            commonService.createObject(orderOpLog);
        } catch (Exception e) {
            logger.error("e",e);
        }
    }

    /**
     * 打印厨房订单
     * @param order
     * @param orderOpLog
     */
    public void printKitchenOrder(Order order, OrderOpLog orderOpLog) {
        try {
            List<String> contentList = new ArrayList<>();
            LinkedHashMap<String, String> map = new LinkedHashMap<>();
            map.put("打印了廚房訂單", order.getId().toString());
            Object obj = map;
            String stringObj = JSON.toJSON(obj).toString();
            contentList.add(stringObj);
            String value = JSON.toJSONString(contentList);
            orderOpLog.setDetailContent(value);
            commonService.createObject(orderOpLog);
        } catch (Exception e) {
            logger.error("e",e);
        }
    }

    /**
     * 开台
     * @param order
     * @param orderOpLog
     */
    public void openFoodTable(Order order, OrderOpLog orderOpLog) {
        try {
            List<String> contentList = new ArrayList<>();
            LinkedHashMap<String, String> map = new LinkedHashMap<>();
            map.put("開台", order.getId().toString());
            Object obj = map;
            String stringObj = JSON.toJSON(obj).toString();
            contentList.add(stringObj);
            String value = JSON.toJSONString(contentList);
            orderOpLog.setDetailContent(value);
            commonService.createObject(orderOpLog);
        } catch (Exception e) {
            logger.error("e",e);
        }
    }

    /**
     * 取消开台
     * @param order
     * @param orderOpLog
     */
    public void cancelFoodTable(Order order, OrderOpLog orderOpLog) {
        try {
            List<String> contentList = new ArrayList<>();
            LinkedHashMap<String, String> map = new LinkedHashMap<>();
            map.put("取消開台", order.getId().toString());
            Object obj = map;
            String stringObj = JSON.toJSON(obj).toString();
            contentList.add(stringObj);
            String value = JSON.toJSONString(contentList);
            orderOpLog.setDetailContent(value);
            commonService.createObject(orderOpLog);
        } catch (Exception e) {
            logger.error("e",e);
        }
    }

    /**
     * 失效订单
     * @param order
     * @param orderOpLog
     */
    public void expirationOrderLog(Order order, OrderOpLog orderOpLog) {
        try {
            List<String> contentList = new ArrayList<>();
            LinkedHashMap<String, String> map = new LinkedHashMap<>();
            map.put("訂單失效了", order.getId().toString());
            Object obj = map;
            String stringObj = JSON.toJSON(obj).toString();
            contentList.add(stringObj);
            String value = JSON.toJSONString(contentList);
            orderOpLog.setDetailContent(value);
            commonService.createObject(orderOpLog);
        } catch (Exception e) {
            logger.error("e",e);
        }
    }

    /**
     *
     * @param order
     * @param orderOpLog
     */
    public void userAllPay(Order order, OrderOpLog orderOpLog) {
        try {
            List<String> contentList = new ArrayList<>();
            LinkedHashMap<String, String> map = new LinkedHashMap<>();
            map.put("進入全部結賬", order.getId().toString());
            Object obj = map;
            String stringObj = JSON.toJSON(obj).toString();
            contentList.add(stringObj);
            String value = JSON.toJSONString(contentList);
            orderOpLog.setDetailContent(value);
            commonService.createObject(orderOpLog);
        } catch (Exception e) {
            logger.error("e",e);
        }
    }


    /**
     * 搭台
     * @param order
     * @param orderOpLog
     */
    public void takeFoodTable(Order order, OrderOpLog orderOpLog) {
        try {
            List<String> contentList = new ArrayList<>();
            LinkedHashMap<String, String> map = new LinkedHashMap<>();
            map.put("进行了搭台", order.getId().toString());
            map.put("台号", order.getCode()+"-"+order.getShowNo());
            Object obj = map;
            String stringObj = JSON.toJSON(obj).toString();
            contentList.add(stringObj);
            String value = JSON.toJSONString(contentList);
            orderOpLog.setDetailContent(value);
            commonService.createObject(orderOpLog);
        } catch (Exception e) {
            logger.error("e",e);
        }
    }

    /**
     * 转台
     * @param order
     * @param foodTableTemplate
     * @param orderOpLog
     */
    public void moveFoodTable(Order order, FoodTable foodTableTemplate, OrderOpLog orderOpLog) {
        try {
            List<String> contentList = new ArrayList<>();
            LinkedHashMap<String, String> map = new LinkedHashMap<>();
            map.put("进行了转台", order.getId().toString());
            map.put(order.getCode()+"：转移到", foodTableTemplate.getCode());
            Object obj = map;
            String stringObj = JSON.toJSON(obj).toString();
            contentList.add(stringObj);
            String value = JSON.toJSONString(contentList);
            orderOpLog.setDetailContent(value);
            commonService.createObject(orderOpLog);
        } catch (Exception e) {
            logger.error("e",e);
        }
    }

    /**
     * c端确认落单
     * @param order
     * @param orderOpLog
     */
    public void confirmOrder(Order order, OrderOpLog orderOpLog) {
        try {
            List<String> contentList = new ArrayList<>();
            LinkedHashMap<String, String> map = new LinkedHashMap<>();
            map.put("确认落单了", order.getId().toString());
            Object obj = map;
            String stringObj = JSON.toJSON(obj).toString();
            contentList.add(stringObj);
            String value = JSON.toJSONString(contentList);
            orderOpLog.setDetailContent(value);
            commonService.createObject(orderOpLog);
        } catch (Exception e) {
            logger.error("e",e);
        }
    }

    /**
     * 清空了购物车
     * @param order
     * @param orderOpLog
     */
    public void cleanOrderItem(Order order, OrderOpLog orderOpLog) {
        try {
            List<String> contentList = new ArrayList<>();
            LinkedHashMap<String, String> map = new LinkedHashMap<>();
            map.put("清空了购物车", order.getId().toString());
            Object obj = map;
            String stringObj = JSON.toJSON(obj).toString();
            contentList.add(stringObj);
            String value = JSON.toJSONString(contentList);
            orderOpLog.setDetailContent(value);
            commonService.createObject(orderOpLog);
        } catch (Exception e) {
            logger.error("e",e);
        }
    }

    /**
     * 减少落单审核里面的菜
     *
     * @param orderItem
     * @param orderOpLogContent
     * @return
     */
    public OrderOpLog subOrderAuditOrderItem(OrderItem orderItem, OrderOpLog orderOpLogContent) {
        try {
            List<String> contentList = new ArrayList<>();
            LinkedHashMap<String, String> map = new LinkedHashMap<>();
            orderOpLogContent.setAddItem(true);
            orderOpLogContent.setItemName(orderItem.getItemName());
            map.put("名称", orderItem.getItemName());
            map.put("數量", "-1");
            Object obj = map;
            String stringObj = JSON.toJSON(obj).toString();
            contentList.add(stringObj);
            String jsonString = disposeDetail(orderItem, contentList);
            orderOpLogContent.setDetailContent(jsonString);
            commonService.createObject(orderOpLogContent);
        } catch (Exception e) {
            logger.error("e", e);
        }
        return orderOpLogContent;
    }

    /**
     * 删除落单审核里面的菜
     *
     * @param orderItem
     * @param orderOpLogContent
     * @return
     */
    public OrderOpLog deleteOrderAuditOrderItem(OrderItem orderItem, OrderOpLog orderOpLogContent) {
        try {
            List<String> contentList = new ArrayList<>();
            LinkedHashMap<String, String> map = new LinkedHashMap<>();
            orderOpLogContent.setAddItem(true);
            orderOpLogContent.setItemName(orderItem.getItemName());
            map.put("名称", orderItem.getItemName());
            map.put("數量", getInt(orderItem.getCount()).toString());
            Object obj = map;
            String stringObj = JSON.toJSON(obj).toString();
            contentList.add(stringObj);
            String jsonString = disposeDetail(orderItem, contentList);
            orderOpLogContent.setDetailContent(jsonString);
            commonService.createObject(orderOpLogContent);
        } catch (Exception e) {
            logger.error("e", e);
        }
        return orderOpLogContent;
    }


    /**
     * 更新了购物车B端处理
     *
     * @param reqOrderItem
     * @param orderInDb
     * @param customerRequest
     * @param orderOpLog
     */
    public void updateOrderItem_B(OrderItem reqOrderItem, Order orderInDb, Boolean customerRequest, OrderOpLog orderOpLog) {
        try {
            //判断B端是否有相同的orderItem
            OrderItem sameOrderItem = findSameOrderItem_B(reqOrderItem, orderInDb, customerRequest);
            List<OrderItem> orderItems = new ArrayList<>();
            //有相同的
            if (isNotEmpty(sameOrderItem)) {
                List<String> contentList = new ArrayList<>();
                LinkedHashMap<String, String> map = new LinkedHashMap<>();
                map.put("更新购物车", "该购物车没有发生变化");
                map.put("名称", sameOrderItem.getItemName_zh());
                Object obj = map;
                String stringObj = JSON.toJSON(obj).toString();
                contentList.add(stringObj);
                String value = JSON.toJSONString(contentList);
                orderOpLog.setDetailContent(value);
                commonService.createObject(orderOpLog);

            } else {
                 //没有相同就 记录改变的这个orderItem
                List<String> contentList = new ArrayList<>();
                LinkedHashMap<String, String> map = new LinkedHashMap<>();
                map.put("更新购物车", "该购物车改变为以下");
                map.put("名称", reqOrderItem.getItemName_zh());
                map.put("數量", reqOrderItem.getCount().toString());
                Object obj = map;
                String stringObj = JSON.toJSON(obj).toString();
                contentList.add(stringObj);
                String jsonString = disposeDetail(reqOrderItem, contentList);
                orderOpLog.setDetailContent(jsonString);
                commonService.createObject(orderOpLog);
            }

        } catch (Exception e) {
            logger.error("e", e);
        }
    }

    /**
     * 判断B端是否有一样的orderItem
     *
     * @param reqOrderItem
     * @param customerRequest
     * @param orderInDb
     * @return
     */
    public OrderItem findSameOrderItem_B(OrderItem reqOrderItem, Order orderInDb, Boolean customerRequest) {
        try {
            //不是C端请求
            if (getBoolean(customerRequest)) {
                logger.info("not the customer request");
                return null;
            }

            //数据库里面还是空的,就不做判断
            List<OrderItem> orderItemDetailList = getList(orderInDb.getOrderItemDetailList());
            if (isEmpty(orderItemDetailList)) {
                logger.info("order item detail list is empty");
                return null;
            }

            List<Long> sendKithenOrderItemIdList = getList(orderInDb.getSendPrinterList()).stream().map(p -> p.getOrderItemId()).collect(Collectors.toList());
            //查询相同项
            Optional<OrderItem> orderItemOptional = orderItemDetailList.stream().filter(orderItem -> {
                //找到了，还要是未发送到厨房的,也没有落单的菜
                return getBoolean(orderItem.getCheckout()) && getBoolean(orderItem.getConfirmItem()) && !sendKithenOrderItemIdList.contains(orderItem.getId()) && equals(reqOrderItem, orderItem);
            }).findFirst();

            //找到了
            if (orderItemOptional.isPresent()) {
                return orderItemOptional.get();
            }
        } catch (Exception e) {
            logger.error("e", e);
        }

        return null;
    }


    /**
     * C端购物车日志记录
     * @param OrderItemList
     * @param orderOpLog
     */
    public void updateOrderItemChange_C(List<OrderItem> OrderItemList, OrderOpLog orderOpLog) {
        try {
            JSONArray jsonArray = new JSONArray();
            for (OrderItem orderItem : OrderItemList) {
                List<String> contentList = new ArrayList<>();
                LinkedHashMap<String, String> map = new LinkedHashMap<>();
                orderOpLog.setAddItem(true);
                orderOpLog.setItemName(orderItem.getItemName());
                map.put("更新购物车", "该购物车改变为以下");
                map.put("名称", orderItem.getItemName());
                map.put("數量", orderItem.getCount().toString());
                Object obj = map;
                String stringObj = JSON.toJSON(obj).toString();
                contentList.add(stringObj);
                String disposeDetail = disposeDetail(orderItem, contentList);
                //处理多个json对象
                JSONArray contextJson = JSONObject.parseArray(disposeDetail);
                jsonArray.addAll(contextJson);
            }
            String jsonToString = JSON.toJSON(jsonArray).toString();
            orderOpLog.setDetailContent(jsonToString);
            commonService.createObject(orderOpLog);
        } catch (Exception e) {
            logger.error("e", e);
        }
    }


    /**
     * C端更新购物车 记录没有变化的orderItem
     * @param OrderItem
     * @param orderOpLog
     */
    public void updateOrderItem_C(OrderItem OrderItem, OrderOpLog orderOpLog) {
        try {
            List<String> contentList = new ArrayList<>();
            LinkedHashMap<String, String> map = new LinkedHashMap<>();
            map.put("更新购物车", "该购物车没有发生变化");
            map.put("名称", OrderItem.getItemName_zh());
            Object obj = map;
            String stringObj = JSON.toJSON(obj).toString();
            contentList.add(stringObj);
            String value = JSON.toJSONString(contentList);
            orderOpLog.setDetailContent(value);
            commonService.createObject(orderOpLog);
        } catch (Exception e) {
            logger.error("e", e);
        }
    }

}
